package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.xuanzheng.company.entity.Task;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.TaskService;
import com.xuanzheng.company.service.impl.TaskServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.stream.Collectors;

@WebServlet("/api/task/update-score")
public class UpdateTaskScoreServlet extends HttpServlet {
    private final TaskService taskService = new TaskServiceImpl();
    private final Gson gson = new Gson();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        JsonObject result = new JsonObject();

        try {
            // 检查用户是否登录
            HttpSession session = req.getSession();
            User user = (User) session.getAttribute("user");
            if (user == null) {
                result.addProperty("success", false);
                result.addProperty("message", "请先登录");
                out.print(gson.toJson(result));
                return;
            }

            // 检查用户是否是公司负责人
            if (user.getRole() != 1) {
                result.addProperty("success", false);
                result.addProperty("message", "只有公司负责人可以修改任务评分");
                out.print(gson.toJson(result));
                return;
            }

            // 获取请求体
            String requestBody = req.getReader().lines().collect(Collectors.joining());
            JsonObject requestJson = gson.fromJson(requestBody, JsonObject.class);
            String taskId = requestJson.get("taskId").getAsString();
            int newScore = requestJson.get("score").getAsInt();

            // 获取任务信息
            Task task = taskService.getTaskById(taskId);
            if (task == null) {
                result.addProperty("success", false);
                result.addProperty("message", "任务不存在");
                out.print(gson.toJson(result));
                return;
            }

            // 检查是否是任务的派发者
            if (!user.getId().equals(task.getManagerId())) {
                result.addProperty("success", false);
                result.addProperty("message", "您不是该任务的派发者");
                out.print(gson.toJson(result));
                return;
            }

            // 检查任务状态是否为已完成(2)
            if (task.getStatus() != 2) {
                result.addProperty("success", false);
                result.addProperty("message", "只能修改已完成任务的评分");
                out.print(gson.toJson(result));
                return;
            }

            // 修改评分
            boolean success = taskService.updateTaskScore(taskId, newScore);
            if (success) {
                result.addProperty("success", true);
                result.addProperty("message", "评分已更新为: " + newScore);
            } else {
                result.addProperty("success", false);
                result.addProperty("message", "评分更新失败");
            }
        } catch (Exception e) {
            result.addProperty("success", false);
            result.addProperty("message", "系统错误: " + e.getMessage());
            e.printStackTrace();
        }

        out.print(gson.toJson(result));
    }
} 